
namespace GHIElectronics.Graphics.Simple128x64
{
  class GPainterExtensions : GHIElectronics.Graphics.Simple128x64.GPainter
  {
    /// <summary>
    /// Clears the text that was previously printed with print.
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="str">Same string as print</param>
    public void PrintClear(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        Print(x + i * 6, y, ' ');
      }
    }
    
    /// <summary>
    /// Centers the text on the screen to print
    /// </summary>
    /// <param name="x">Not used</param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintCentered(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        Print(64 - (str.Length * 6) + ((str.Length * 6) / 2) + (i * 6), y, str[i]);
      }
    }

    /// <summary>
    /// Clears the previously printed centered text
    /// </summary>
    /// <param name="x">Not used</param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintCenteredClear(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        Print(64 - (str.Length * 6) + ((str.Length * 6) / 2) + (i * 6), y, str[i]);
      }
    }

    /// <summary>
    /// Prints right aligned (so x is the sting's end position)
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintRightAligned(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        Print(x - (((str.Length) * 6) - (i * 6)), y, str[i]);
      }
    }

    /// <summary>
    /// Clears the previously right aligned text
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintRightAlignedClear(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        Print(x - (((str.Length) * 6) - (i * 6)), y, ' ');
      }
    }

    /// <summary>
    /// Print small right aligned (so x is the string's end position)
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintSmallRightAligned(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        PrintSmall(x - (((str.Length) * 4) - (i * 4)), y, str[i]);
      }
    }

    /// <summary>
    /// Clears the previously right aligned text
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="str"></param>
    public void PrintSmallRightAlignedClear(int x, int y, string str)
    {
      for (int i = 0; i < str.Length; i++)
      {
        PrintSmall(x - (((str.Length) * 4) - (i * 4)), y, ' ');
      }
    }

    /// <summary>
    /// Helper method for ClearCircle
    /// </summary>
    /// <param name="cx"></param>
    /// <param name="cy"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    private void ClearCirclePoints(int cx, int cy, int x, int y)
    {
      if (x == 0)
      {
        ClearPixel(cx, cy + y);
        ClearPixel(cx, cy - y);
        ClearPixel(cx + y, cy);
        ClearPixel(cx - y, cy);
      }
      else
        if (x == y)
        {
          ClearPixel(cx + x, cy + y);
          ClearPixel(cx - x, cy + y);
          ClearPixel(cx + x, cy - y);
          ClearPixel(cx - x, cy - y);
        }
        else
          if (x < y)
          {
            ClearPixel(cx + x, cy + y);
            ClearPixel(cx - x, cy + y);
            ClearPixel(cx + x, cy - y);
            ClearPixel(cx - x, cy - y);
            ClearPixel(cx + y, cy + x);
            ClearPixel(cx - y, cy + x);
            ClearPixel(cx + y, cy - x);
            ClearPixel(cx - y, cy - x);
          }
    }

    /// <summary>
    /// Clear the circle printed with Circle
    /// </summary>
    /// <param name="xCenter"></param>
    /// <param name="yCenter"></param>
    /// <param name="radius"></param>
    public void ClearCircle(int xCenter, int yCenter, int radius)
    {
      int x = 0;
      int y = radius;
      int p = (5 - radius * 4) / 4;

      ClearCirclePoints(xCenter, yCenter, x, y);
      while (x < y)
      {
        x++;
        if (p < 0)
        {
          p += 2 * x + 1;
        }
        else
        {
          y--;
          p += 2 * (x - y) + 1;
        }
        ClearCirclePoints(xCenter, yCenter, x, y);
      }
    }


  }
}
